from direct.gui.DirectGui import DirectFrame, DirectScrolledList, DirectButton, DirectLabel
from direct.showbase.DirectObject import DirectObject
import direct.gui.DirectGuiGlobals as DGG
import re



class InventoryGui:
    """
    Class responsible for handling inventory GUI.
    
    This way of organizing this has one major downfall - being in the main
    'branch' of code, we might not even be aware that there is any GUI at all!
    Besides: class interacts directly with DirectScrolledFrame, used as 
    inventory GUI, and DirectLabels, used as list entries. 
    
    Class attribs:
    _directObjectParent - DirectObject on which inventory GUI should be displayed
    
    Note: I've tried using a dictionary as a  class param, which would store
    item's name and appropriate label's index, but then realization hit me:
    how am I supposed to know (other than looking directly into the Panda 3D code)
    how DirectScrolledList handles removing of labels? And how should I know
    if this way will (or will not) change in future? So, at least for now,
    the dictionary mentioned above is being constructed on each call of method
    inserting/deleting something from inventory.
    
    """
    
    _directObjectParent = None
    
    def __init__(self, DirectObject):
        """ Init. It calls another method, which actually constructs GUI.
        
            params:
            DirectObject - instance of DirectObject on which inventory GUI should be displayed
        
        """
        
        self._directObjectParent = DirectObject
        self.setUpInventory()
    
    def setUpInventory(self):
        """ Method which constructs GUI. Just after initializing, DirectScrolledFrame
            is visible on the screen, so the method calls hide().
            
        """

        self.numItemsVisible = 4
        self.itemHeight = 0.11

        self.scrolledList = DirectScrolledList(
              decButton_pos= (0.35, 0, 0.53)
            , decButton_text = "/\\"
            , decButton_text_scale = 0.03
            , decButton_borderWidth = (0.005, 0.005)
            
            , incButton_pos= (0.35, 0, -0.02)
            , incButton_text = "\/"
            , incButton_text_scale = 0.03
            , incButton_borderWidth = (0.005, 0.005)
            
            , frameSize = (0.14, 0.56, -0.04, 0.57)
            , frameColor = (0,0,0,1)
            , pos = (-1, 0, 0)
            , numItemsVisible = self.numItemsVisible
            , forceHeight = self.itemHeight
            , itemFrame_frameSize = (-0.2, 0.2, -0.36, 0.1)
            , itemFrame_pos = (0.35, 0, 0.4)
            )
        
        self._directObjectParent.myScrolledList = self.scrolledList
        
        self.hide()
        
    def show(self):
        """ Method-shortcut for showing GUI on the screen. """
        
        self._directObjectParent.myScrolledList.show()

    def hide(self):
        """ Method-shortcut for hiding inventory GUI on the screen. """
        
        self._directObjectParent.myScrolledList.hide()
        
        
    def _addNewItem(self, sName, iQuantity=1):
        """ Method for adding new (non-existing before!) items to inventory.
        
        params:
        sName - name (string) on an item
        iQuantity - quantity (int) of item(s)
        """
        
        sItemEntry = str(sName) + " x" + str(iQuantity)
        label = DirectLabel(text=sItemEntry, text_scale=0.04)
        
        self._directObjectParent.myScrolledList.addItem(label)
        self._directObjectParent.myScrolledList.refresh()
        
    def _removeAll(self):
        """ Method to remove all entries in scrolled list. """
        
        self.scrolledList.removeAllItems()
    
    def reload(self, dictItems):
        """ Method which updates the contents of GUI by removing all entries,
            and then drawing all what's present in dictItems
        """
        
        self._removeAll()
        
        for item in dictItems:
            self._addNewItem(item, dictItems[item]) 
        